package com.example.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import com.example.config.GsBodyConfig;
import com.example.model.EncryptEnvelope;
import com.example.model.EncryptedKeyDataEnvelope;
import com.example.service.NationCaService;
import com.example.util.HttpTool;
import com.example.util.Md5Util;
import com.example.util.SM4DecryptEncryptFile;
import com.example.util.Sm2Util;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import cn.hutool.json.JSONUtil;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

import javax.annotation.Resource;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathFactory;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

/**
 * @ClassName NationCaServiceImpl
 * @Description TODO
 * @Author kun
 * @Date 2024-10-22 10:55
 * @Version 1.0
 */
@Service
@Slf4j
public class NationCaServiceImpl implements NationCaService {

    private final  String uploadDir = "uploads";
    private final String decryptDir = "decrypts";
    private final String unzipDir = "unzips";
    private final static Logger logger= LoggerFactory.getLogger(NationCaServiceImpl.class);
    //36.142.16.73 120.27.249.148
    private final String naApiUrl="http://36.142.16.73:8081/caapi";
    private final String platFormCode="12620000224333349J";
    private final String systemCode="Body001";

    private final String pubKey="0230a768c7dae8b2d729a11806001e080607eed3047514f92d181b93e71e0d9728";
    private final String priKey="ad9e1083c6f140b39a9dfcf96804fd58ff9120efad6ccbac9861a90beddb61f3";

    @Resource
    private HttpTool httpTool;

    @Resource
    private SM4DecryptEncryptFile sm4DecryptEncryptFile;

    @Override
    public String getNaConfig() {
        Map<String,Object> params=new HashMap<>();
        params.put("caShareComponentsCode","");
        String postStr=JSONUtil.toJsonStr(params);

        postStr=Sm2Util.encrypt1(pubKey,postStr);

        String sign= Sm2Util.sign(priKey,postStr);

        Map<String,String> headers=new HashMap<>();
        headers.put("platFormCode",platFormCode);
        headers.put("systemCode",systemCode);
        headers.put("signValue",sign);

        String result=httpTool.post(naApiUrl+"/config/get",headers,postStr);
        if(StrUtil.isNotEmpty(result)&&result.startsWith("04")){
            result=Sm2Util.decrypt1(priKey,result);
        }
        return result;
    }

    @Override
    public String getLoginQrCode() {
        Map<String,Object> params=new HashMap<>();
        //从getNaConfig的platformCode获取
        params.put("caShareComponentsCode","D6200005491");
        String postStr=JSONUtil.toJsonStr(params);

        postStr=Sm2Util.encrypt1(pubKey,postStr);

        String sign= Sm2Util.sign(priKey,postStr);

        Map<String,String> headers=new HashMap<>();
        headers.put("platFormCode",platFormCode);
        headers.put("systemCode",systemCode);
        headers.put("signValue",sign);

        String result=httpTool.post(naApiUrl+"/sharedbody/lginQrCode",headers,postStr);
        if(StrUtil.isNotEmpty(result)&&result.startsWith("04")){
            result=Sm2Util.decrypt1(priKey,result);
        }
        return result;
    }

    /**
     * 获取登录二维码扫描状态
     * @param tid
     * @return
     */
    @Override
    public String queryScannedStatus(String tid) {

        Map<String,Object> params=new HashMap<>();
        //从getNaConfig的platformCode获取
        params.put("tid",tid);
        String postStr=JSONUtil.toJsonStr(params);

        postStr=Sm2Util.encrypt1(pubKey,postStr);

        String sign= Sm2Util.sign(priKey,postStr);

        Map<String,String> headers=new HashMap<>();
        headers.put("platFormCode",platFormCode);
        headers.put("systemCode",systemCode);
        headers.put("signValue",sign);

        String result=httpTool.post(naApiUrl+"/sharedbody/queryScannedStatus",headers,postStr);
        if(StrUtil.isNotEmpty(result)&&result.startsWith("04")){
            result=Sm2Util.decrypt1(priKey,result);
        }
        return result;
    }
    @Override
    public String getUserInfo(String tid) {

        Map<String,Object> params=new HashMap<>();
        //从getNaConfig的platformCode获取
        params.put("tid",tid);
        String postStr=JSONUtil.toJsonStr(params);

        postStr=Sm2Util.encrypt1(pubKey,postStr);

        String sign= Sm2Util.sign(priKey,postStr);

        Map<String,String> headers=new HashMap<>();
        headers.put("platFormCode",platFormCode);
        headers.put("systemCode",systemCode);
        headers.put("signValue",sign);

        String result=httpTool.post(naApiUrl+"/sharedbody/getUserAuthInfo",headers,postStr);
        if(StrUtil.isNotEmpty(result)&&result.startsWith("04")){
            result=Sm2Util.decrypt1(priKey,result);
        }
        return result;
    }

    @Override
    public String getEnQrCode() {

        Map<String,Object> params=new HashMap<>();
        //从getNaConfig的platformCode获取
        params.put("caShareComponentsCode","D6200005491");
        String postStr=JSONUtil.toJsonStr(params);

        postStr=Sm2Util.encrypt1(pubKey,postStr);

        String sign= Sm2Util.sign(priKey,postStr);

        Map<String,String> headers=new HashMap<>();
        headers.put("platFormCode",platFormCode);
        headers.put("systemCode",systemCode);
        headers.put("signValue",sign);

        String result=httpTool.post(naApiUrl+"/msgencrypt/getQrcode",headers,postStr);
        if(StrUtil.isNotEmpty(result)&&result.startsWith("04")){
            result=Sm2Util.decrypt1(priKey,result);
        }
        return result;
    }

    @Override
    public String queryEnScannedStatus(String tid){
        Map<String,Object> params=new HashMap<>();
        //从getNaConfig的platformCode获取
        params.put("tid",tid);
        String postStr=JSONUtil.toJsonStr(params);

        postStr=Sm2Util.encrypt1(pubKey,postStr);

        String sign= Sm2Util.sign(priKey,postStr);

        Map<String,String> headers=new HashMap<>();
        headers.put("platFormCode",platFormCode);
        headers.put("systemCode",systemCode);
        headers.put("signValue",sign);

        String result=httpTool.post(naApiUrl+"/msgencrypt/queryQrcodeStatus",headers,postStr);
        if(StrUtil.isNotEmpty(result)&&result.startsWith("04")){
            result=Sm2Util.decrypt1(priKey,result);
        }
        return result;
    }

    @Override
    public String encryptMsg(String tid, String msg) {
        Map<String,Object> params=new HashMap<>();
        //从getNaConfig的platformCode获取
        params.put("tid",tid);
        params.put("msg",msg);
        String postStr=JSONUtil.toJsonStr(params);

        postStr=Sm2Util.encrypt1(pubKey,postStr);

        String sign= Sm2Util.sign(priKey,postStr);

        Map<String,String> headers=new HashMap<>();
        headers.put("platFormCode",platFormCode);
        headers.put("systemCode",systemCode);
        headers.put("signValue",sign);

        String result=httpTool.post(naApiUrl+"/msgencrypt/ebncrypt",headers,postStr);
        if(StrUtil.isNotEmpty(result)&&result.startsWith("04")){
            result=Sm2Util.decrypt1(priKey,result);
        }
        return result;
    }

    /**
     * 获取解密二维码
     * @return
     */
    @Override
    public String getDeQrCode(String decyptUserType,String threeInOneCode,String idCardHash) {
        Map<String,Object> params=new HashMap<>();
        //从getNaConfig的platformCode获取
        params.put("decyptUserType",decyptUserType);
        params.put("threeInOneCode",threeInOneCode);
        params.put("idCardHash",idCardHash);
        String postStr=JSONUtil.toJsonStr(params);

        postStr=Sm2Util.encrypt1(pubKey,postStr);

        String sign= Sm2Util.sign(priKey,postStr);

        Map<String,String> headers=new HashMap<>();
        headers.put("platFormCode",platFormCode);
        headers.put("systemCode",systemCode);
        headers.put("signValue",sign);

        String result=httpTool.post(naApiUrl+"/decrypt/getQrCode",headers,postStr);
        if(StrUtil.isNotEmpty(result)&&result.startsWith("04")){
            result=Sm2Util.decrypt1(priKey,result);
        }
        return result;
    }

    @Override
    public String queryDeScannedStatus(String tid) {
        Map<String,Object> params=new HashMap<>();
        //从getNaConfig的platformCode获取
        params.put("tid",tid);
        String postStr=JSONUtil.toJsonStr(params);

        postStr=Sm2Util.encrypt1(pubKey,postStr);

        String sign= Sm2Util.sign(priKey,postStr);

        Map<String,String> headers=new HashMap<>();
        headers.put("platFormCode",platFormCode);
        headers.put("systemCode",systemCode);
        headers.put("signValue",sign);

        String result=httpTool.post(naApiUrl+"/decrypt/quyeryDecryptStatus",headers,postStr);
        if(StrUtil.isNotEmpty(result)&&result.startsWith("04")){
            result=Sm2Util.decrypt1(priKey,result);
        }
        return result;
    }
    @Override
    public String getDecryptUserInfo(String tid) {
        Map<String,Object> params=new HashMap<>();
        //从getNaConfig的platformCode获取
        params.put("tid",tid);
        String postStr=JSONUtil.toJsonStr(params);

        postStr=Sm2Util.encrypt1(pubKey,postStr);

        String sign= Sm2Util.sign(priKey,postStr);

        Map<String,String> headers=new HashMap<>();
        headers.put("platFormCode",platFormCode);
        headers.put("systemCode",systemCode);
        headers.put("signValue",sign);

        String result=httpTool.post(naApiUrl+"/decrypt/getDecyptUserInfo",headers,postStr);
        if(StrUtil.isNotEmpty(result)&&result.startsWith("04")){
            result=Sm2Util.decrypt1(priKey,result);
        }
        return result;
    }

    @Override
    public String uploadEncryptEnvelope(EncryptEnvelope encryptEnvelope) {
        String postStr=JSONUtil.toJsonStr(encryptEnvelope);
        postStr=Sm2Util.encrypt1(pubKey,postStr);
        String sign= Sm2Util.sign(priKey,postStr);
        Map<String,String> headers=new HashMap<>();
        headers.put("platFormCode",platFormCode);
        headers.put("systemCode",systemCode);
        headers.put("signValue",sign);
        String result=httpTool.post(naApiUrl+"/decrypt/uploadEnvelope",headers,postStr);
        if(StrUtil.isNotEmpty(result)&&result.startsWith("04")){
            result=Sm2Util.decrypt1(priKey,result);
        }
        return result;
    }

    /**
     * 获取解密消息
     * @param tid
     * @return
     */
    @Override
    public String getDecryptMsg(String tid) {
        Map<String,Object> params=new HashMap<>();
        //从getNaConfig的platformCode获取
        params.put("tid",tid);
        String postStr=JSONUtil.toJsonStr(params);

        postStr=Sm2Util.encrypt1(pubKey,postStr);

        String sign= Sm2Util.sign(priKey,postStr);

        Map<String,String> headers=new HashMap<>();
        headers.put("platFormCode",platFormCode);
        headers.put("systemCode",systemCode);
        headers.put("signValue",sign);

        String result=httpTool.post(naApiUrl+"/decrypt/getDecryptedData",headers,postStr);
        if(StrUtil.isNotEmpty(result)&&result.startsWith("04")){
            result=Sm2Util.decrypt1(priKey,result);
        }
        return result;
    }

    /**
     * 主体共享平台登录
     * @param threeInOneCode 统一社会信用代码或身份证号
     * @return
     * */
    public JSONObject gsBodyLogin(String threeInOneCode){
        String date= DateUtil.format(new java.util.Date(),"yyyy-MM-dd HH:mm:ss");
        String sign= Md5Util.md5(threeInOneCode+date+GsBodyConfig.getInstance().getGsSalt()) ;
        //设置请求头
        Map<String,String> headers=new HashMap<>();
        headers.put("PlatForm", GsBodyConfig.getInstance().getGsPlatformCode());

        //设置请求参数
        JSONObject requestBody=new JSONObject();
        requestBody.put("ThreeCode",threeInOneCode);
        requestBody.put("PlatForm",GsBodyConfig.getInstance().getGsPlatformCode());
        requestBody.put("Date",date);
        requestBody.put("Sign",sign);
        requestBody.put("AuthorizationCode",GsBodyConfig.getInstance().getAuthCode());


        String requestBodyStr= requestBody.toString();

        //加密请求参数
        requestBodyStr= Sm2Util.encrypt1(GsBodyConfig.getInstance().getGsPubKey(),requestBodyStr);

        String apiUrl=GsBodyConfig.getInstance().getGsApiUrl()+"/PermissionNewInterfacec/BiddingThreeCodeLogin";
        String postResult= httpTool.post (apiUrl,headers,requestBodyStr);

        postResult= Sm2Util.decrypt1(GsBodyConfig.getInstance().getGsPriKey(),postResult);
        logger.info("查询主体共享平台信息返回结果:{}",postResult);
        JSONObject result=new JSONObject(postResult);

        Integer mark=result.getInt("mark");
        switch (mark){
            case 1:
                //成功
                return result;
            case -1:
                result.put("exceptioninfo","主体共享平台未授权");
                return result;
            case -3:
                result.put("exceptioninfo","未在甘肃省主体共享平台注册,请先注册");
                result.put("redirectUrl",GsBodyConfig.getInstance().getGsApiUrl());
                return result;
            case -7:
                result.put("exceptioninfo","未在甘肃省主体共享平台注册,请先注册");
                result.put("redirectUrl",GsBodyConfig.getInstance().getGsApiUrl());
                return result;
            default:
                return result;
        }
    }

    /**
     * 获取签名二维码
     * @return
     */
    @Override
    public String getSignQrCode(String msg) {
        Map<String,Object> params=new HashMap<>();
        //从getNaConfig的platformCode获取
        params.put("msg",msg);
        String postStr=JSONUtil.toJsonStr(params);

        postStr=Sm2Util.encrypt1(pubKey,postStr);

        String sign= Sm2Util.sign(priKey,postStr);

        Map<String,String> headers=new HashMap<>();
        headers.put("platFormCode",platFormCode);
        headers.put("systemCode",systemCode);
        headers.put("signValue",sign);

        String result=httpTool.post(naApiUrl+"/sign/getQrCode",headers,postStr);
        if(StrUtil.isNotEmpty(result)&&result.startsWith("04")){
            result=Sm2Util.decrypt1(priKey,result);
        }
        return result;
    }

    @Override
    public String querySignScannedStatus(String tid) {
        Map<String,Object> params=new HashMap<>();
        //从getNaConfig的platformCode获取
        params.put("tid",tid);
        String postStr=JSONUtil.toJsonStr(params);

        postStr=Sm2Util.encrypt1(pubKey,postStr);

        String sign= Sm2Util.sign(priKey,postStr);

        Map<String,String> headers=new HashMap<>();
        headers.put("platFormCode",platFormCode);
        headers.put("systemCode",systemCode);
        headers.put("signValue",sign);

        String result=httpTool.post(naApiUrl+"/sign/queryQrcodeStatus",headers,postStr);
        if(StrUtil.isNotEmpty(result)&&result.startsWith("04")){
            result=Sm2Util.decrypt1(priKey,result);
        }
        return result;
    }

    @Override
    public String getSignResult(String tid) {
        Map<String,Object> params=new HashMap<>();
        //从getNaConfig的platformCode获取
        params.put("tid",tid);
        String postStr=JSONUtil.toJsonStr(params);

        postStr=Sm2Util.encrypt1(pubKey,postStr);

        String sign= Sm2Util.sign(priKey,postStr);

        Map<String,String> headers=new HashMap<>();
        headers.put("platFormCode",platFormCode);
        headers.put("systemCode",systemCode);
        headers.put("signValue",sign);

        String result=httpTool.post(naApiUrl+"/sign/getSignResult",headers,postStr);
        if(StrUtil.isNotEmpty(result)&&result.startsWith("04")){
            result=Sm2Util.decrypt1(priKey,result);
        }
        return result;
    }

    @Override
    public String verifySign(String msg, String sign, String alogrithm) {
        Map<String,Object> params=new HashMap<>();
        //从getNaConfig的platformCode获取
        params.put("msg",msg);
        params.put("signature",sign);
        params.put("algorithm",alogrithm);
        String postStr=JSONUtil.toJsonStr(params);

        postStr=Sm2Util.encrypt1(pubKey,postStr);

        String signValue= Sm2Util.sign(priKey,postStr);

        Map<String,String> headers=new HashMap<>();
        headers.put("platFormCode",platFormCode);
        headers.put("systemCode",systemCode);
        headers.put("signValue",signValue);

        String result=httpTool.post(naApiUrl+"/sign/verifySign",headers,postStr);
        if(StrUtil.isNotEmpty(result)&&result.startsWith("04")){
            result=Sm2Util.decrypt1(priKey,result);
        }
        return result;
    }

    @Override
    public String getSignPdfCallback(String platFormCode, String systemCode, String fileKey) {
        Map<String,Object> params=new HashMap<>();
        //从getNaConfig的platformCode获取
        params.put("fileKey",fileKey);
        String postStr=JSONUtil.toJsonStr(params);

        postStr=Sm2Util.encrypt1(pubKey,postStr);

        String signValue= Sm2Util.sign(priKey,postStr);

        Map<String,String> headers=new HashMap<>();
        headers.put("platFormCode",platFormCode);
        headers.put("systemCode",systemCode);
        headers.put("signValue",signValue);

        String result=httpTool.post(naApiUrl+"/sign/getSignPdfCallback",headers,postStr);
        if(StrUtil.isNotEmpty(result)&&result.startsWith("04")){
            result=Sm2Util.decrypt1(priKey,result);
        }
        return result;
    }

    private void unzip(String zipFilePath, String outputFolder) throws IOException {
        byte[] buffer = new byte[1024];
        Path zipPath = Paths.get(zipFilePath);
        Path destDir = Paths.get(outputFolder);

        try (ZipInputStream zis = new ZipInputStream(new FileInputStream(zipPath.toFile()))) {
            ZipEntry zipEntry = zis.getNextEntry();
            while (zipEntry != null) {
                Path newFile = destDir.resolve(zipEntry.getName());
                Files.createDirectories(newFile.getParent());
                if (zipEntry.isDirectory()) {
                    Files.createDirectory(newFile);
                } else {
                    try (FileOutputStream fos = new FileOutputStream(newFile.toFile())) {
                        int len;
                        while ((len = zis.read(buffer)) > 0) {
                            fos.write(buffer, 0, len);
                        }
                    }
                }
                zipEntry = zis.getNextEntry();
            }
            zis.closeEntry();
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public Map decryptUnzipFile(String fileKey){
        File destFile = new File(uploadDir, fileKey);
        if(!destFile.exists()){
            return null;
        }
        Map<String,Object> result=new HashMap<>();
        //解压zip文件
        try {
            String outDir=unzipDir+File.separator+fileKey.substring(0,fileKey.lastIndexOf("."));
            File dir = new File(outDir);
            if (!dir.exists()) {
                dir.mkdirs();
            }
            unzip(destFile.getAbsolutePath(),outDir);
            File xmlFile=new File(outDir+"/Envelope.xml") ;

            String xmlContent = new String(Files.readAllBytes(xmlFile.toPath()));
            DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
            InputSource is = new InputSource(new StringReader(xmlContent));
            Document doc = dBuilder.parse(is);

            // 使用XPath提取所需字段
            XPath xPath = XPathFactory.newInstance().newXPath();
            String orgCode = xPath.compile("//ORG_CODE").evaluate(doc);
            String idCardHash = xPath.compile("//ID_CARD_HASH").evaluate(doc);
            String encryptionUserType = xPath.compile("//ENCRYPTION_USER_TYPE").evaluate(doc);


            result.put("orgCode",orgCode);
            result.put("idCardHash",idCardHash);
            result.put("decyptUserType",encryptionUserType);

            List<EncryptedKeyDataEnvelope> list=new ArrayList<>();

            // 提取 ENCRYPTED_KEY_DATA_ENVELOPE_LIST 下的字段
            NodeList nodeList = (NodeList) xPath.compile("//ENCRYPTED_KEY_DATA_ENVELOPE_LIST").evaluate(doc, XPathConstants.NODESET);
            for (int i = 0; i < nodeList.getLength(); i++) {
                org.w3c.dom.Node node = nodeList.item(i);
                String symmetricKeyAlgorithm = xPath.compile("SYMMETRIC_KEY_ALGORITHM").evaluate(node);
                String encryptedKeyDataEnvelopeId = xPath.compile("ENCRYPTED_KEY_DATA_ENVELOPE_ID").evaluate(node);
                String encryptionKey = xPath.compile("ENCRYPTION_KEY").evaluate(node);
                String sourceFile = xPath.compile("SOURCE_FILE").evaluate(node);
                String sourceFileHash = xPath.compile("SOURCE_FILE_HASH").evaluate(node);
                String encryptedFile = xPath.compile("ENCRYPTED_FILE").evaluate(node);
                String encryptedFileHash = xPath.compile("ENCRYPTED_FILE_HASH").evaluate(node);
                String caOrgCode = xPath.compile("CA_ORG_CODE").evaluate(node);
                String encryptionCertSn = xPath.compile("ENCRYPTION_CERT_SN").evaluate(node);
                String publicKeyAlgorithm = xPath.compile("PUBLIC_KEY_ALGORITHM").evaluate(node);
//                // 输出提取的信息
//
//                System.out.println("SYMMETRIC_KEY_ALGORITHM: " + symmetricKeyAlgorithm);
//                System.out.println("ENCRYPTED_KEY_DATA_ENVELOPE_ID: " + encryptedKeyDataEnvelopeId);
//                System.out.println("ENCRYPTION_KEY: " + encryptionKey);
//                System.out.println("SOURCE_FILE: " + sourceFile);
//                System.out.println("SOURCE_FILE_HASH: " + sourceFileHash);
//                System.out.println("ENCRYPTED_FILE: " + encryptedFile);
//                System.out.println("ENCRYPTED_FILE_HASH: " + encryptedFileHash);
//                System.out.println("CA_ORG_CODE: " + caOrgCode);
//                System.out.println("ENCRYPTION_CERT_SN: " + encryptionCertSn);
//                System.out.println("PUBLIC_KEY_ALGORITHM: " + publicKeyAlgorithm);

                EncryptedKeyDataEnvelope e=new EncryptedKeyDataEnvelope();
                e.setEncryptedKeyDataEnvelopeId(encryptedKeyDataEnvelopeId);
                e.setCaOrgCode(caOrgCode);
                e.setEncryptionCertSn(encryptionCertSn);
                e.setEncryptionKey(encryptionKey);
                e.setPublicKeyAlgorithm(publicKeyAlgorithm);
                list.add(e);
            }
            result.put("encryptedKeyDataEnvelopeList",list);
            return result;

        }
        catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }
    private  List<String> findEncFiles(Path directory) {
        if (directory == null || !Files.exists(directory)) {
            System.err.println("指定的目录不存在: " + directory);
            return new ArrayList<>();
        }

        List<String> encFiles = new ArrayList<>();
        try {
            Files.walk(directory)
                    .filter(path -> path.toString().endsWith(".enc"))
                    .map(Path::toString)
                    .forEach(encFiles::add);
        } catch (IOException e) {
            e.printStackTrace();
        }

        return encFiles;
    }
    @Override
    public String decryptFile(String fileKey,String decryptKey) {
        String enDir = unzipDir +File.separator+ fileKey.substring(0, fileKey.lastIndexOf("."));
        File dir = new File(enDir);
        if (!dir.exists()) {
            return "";
        }
        List<String> encFiles = findEncFiles(Paths.get(enDir));
        if (encFiles.size() == 0) {
            return "";
        }

        String outDir=decryptDir+File.separator+fileKey.substring(0,fileKey.lastIndexOf("."));
        File outDirFile = new File(outDir);
        if (!outDirFile.exists()) {
            outDirFile.mkdirs();
        }

        String enFile=encFiles.get(0);
        String outFile=outDir+File.separator+enFile.substring(enFile.lastIndexOf(File.separator)+1,enFile.lastIndexOf(".")).replace(".enc","");
        try {
            boolean b=  sm4DecryptEncryptFile.decryptFile(enFile,outFile,decryptKey);
            if(b){
                return outFile;
            }
            return "";
        }catch (Exception e){
            e.printStackTrace();
            return "";
        }
    }
    @Override
    public String uploadFileUrl(String fileUrl,String extName){
        Map<String,String> params = new HashMap<>();
        params.put("fileUrl",fileUrl);
        params.put("extName",extName);
        String postStr=JSONUtil.toJsonStr(params);

        postStr=Sm2Util.encrypt1(pubKey,postStr);

        String signValue= Sm2Util.sign(priKey,postStr);

        Map<String,String> headers=new HashMap<>();
        headers.put("platFormCode",platFormCode);
        headers.put("systemCode",systemCode);
        headers.put("signValue",signValue);

        String result=httpTool.post(naApiUrl+"/fileupload/uploadUrl",headers,postStr);
        if(StrUtil.isNotEmpty(result)&&result.startsWith("04")){
            result=Sm2Util.decrypt1(priKey,result);
        }
        return result;
    }
}
